/*
 * 
 */
package com.roc.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.roc.util.Constance;



/**
 * 
 * @author Administrator
 *
 */
public abstract class BaseSearchCondition implements Serializable{
	 
	private static final long serialVersionUID = -420522674194403989L;
	private List<Object> params=new ArrayList<Object>();
	
	/**
	 * 
	 */
	public BaseSearchCondition() {
		super();
	}
	

	/**
	 * Get the full search HQL.
	 * @param params
	 * @return the String of full search HQL.
	 * @throws Exception
	 */
	public String getFullSearchHQL(List<Object> params) throws Exception{
		StringBuffer fullHQL = new StringBuffer(getSearchHQL());
		fullHQL.append(getSearchConditions(getWhereHQLFieldBeanList(),params));
		fullHQL.append(getOrderPartOfHQL()!=null?" "+getOrderPartOfHQL():"");
		return fullHQL.toString();
	}
	
	/**
	 * Get the full search HQL.
	 * @param params
	 * @return the String of full search HQL.
	 * @throws Exception
	 */
	public String getFullRowCountHQL(List<Object> params) throws Exception{
		StringBuffer fullHQL = new StringBuffer(getRowCountHQL());
		fullHQL.append(getSearchConditions(getWhereHQLFieldBeanList(),params));
		return fullHQL.toString();
	}
	
	/**
	 * Get the HQL search condition by the <code>filedBeanList<code> which offered by the sub class.
	 * @param filedBeanList
	 * @return the <code>StringBuffer</code> of HQL search condition.
	 * @throws Exception
	 */
	public StringBuffer getSearchConditions(List<WhereHQLFieldBean> filedBeanList,List<Object> params) throws Exception{
		if(filedBeanList==null){
			return new StringBuffer("");
		}
		BaseWhereCondition whereCondition = new BaseWhereCondition();
		if(params==null){
			params = new ArrayList<Object>();
		}
		whereCondition.setParams(params);
		for (Iterator<WhereHQLFieldBean> i = filedBeanList.iterator(); i.hasNext();) {
			Object tempObj = i.next();
			if(tempObj==null||!(tempObj instanceof WhereHQLFieldBean)){
				continue;
			}
			WhereHQLFieldBean filedBean = (WhereHQLFieldBean)tempObj;
			
			if(!Constance.Null2EmptyTrim(filedBean.getHQLStr()).equals("")){
				whereCondition.addConditionSQLStr(filedBean.getHQLStr());
			}else if("LIKE".equals(Constance.Null2EmptyTrim(filedBean.getFieldOperation()).toUpperCase().trim())){
				if("FIRST".equals(Constance.Null2EmptyTrim(filedBean.getLikePosition()).toUpperCase().trim())){
					whereCondition.addConditionLikeFirst(
						filedBean.getFieldName(),
						String.valueOf(filedBean.getFieldValue()),
						filedBean.isNullSensitive());
				}else if("END".equals(Constance.Null2EmptyTrim(filedBean.getLikePosition()).toUpperCase().trim())){
					whereCondition.addConditionLikeEnd(
						filedBean.getFieldName(),
						String.valueOf(filedBean.getFieldValue()),
						filedBean.isNullSensitive());
				}else if("ALL".equals(Constance.Null2EmptyTrim(filedBean.getLikePosition()).toUpperCase().trim())){
					whereCondition.addConditionLikeAll(
						filedBean.getFieldName(),
						String.valueOf(filedBean.getFieldValue()),
						filedBean.isNullSensitive());
				}
			}else if("IN".equals(Constance.Null2EmptyTrim(filedBean.getFieldOperation()).toUpperCase().trim())){
				if(!Constance.Null2EmptyTrim(filedBean.getFieldOperation()).trim().equals("")){
					whereCondition.addConditionIn(
						filedBean.getFieldName(),
						filedBean.getFieldValueArray(),
						filedBean.getFieldOperation(),
						filedBean.isNullSensitive());
				}
			}else{
				if(!Constance.Null2EmptyTrim(filedBean.getFieldOperation()).trim().equals("")){
					whereCondition.addCondition(
						filedBean.getFieldName(),
						filedBean.getFieldValue(),
						filedBean.getFieldOperation(),
						filedBean.isNullSensitive());
					this.setParams(whereCondition.getParams());
				}
			}
		}
	
		return whereCondition.getWhereCondition();
	}

	/**
	 * Get the HQL where parameter names for different conditions.
	 * 
	 * @return the String Array of HQL where parameter names
	 */
	protected abstract List<WhereHQLFieldBean> getWhereHQLFieldBeanList();
	/**
	 * An abstract method in order to get the row count hql.
	 * Subclasses of BaseSearchCondition must provide an implementation for this method.
	 * @return row count hql	
	 */
	protected abstract String getRowCountHQL();

	/**
	 * An abstract method in order to get the search hql.
	 * Subclasses of BaseSearchCondition must provide an implementation for this method.
	 * @return search hql	
	 */
	protected abstract String getSearchHQL();

	/**
	 * An abstract method in order to get the order part of search hql .
	 * Subclasses of BaseSearchCondition must provide an implementation for this method.
	 * @return search hql	
	 */
	protected abstract String getOrderPartOfHQL();


	/**
	 * @return the params
	 */
	public List<Object> getParams() {
		return params;
	}


	/**
	 * @param params the params to set
	 */
	public void setParams(List<Object> params) {
		this.params = params;
	}
}
